package me.pinion.kit;

import com.jfinal.kit.PathKit;
import me.pinion.Log;

import java.io.File;
import java.util.HashSet;
import java.util.Set;


public class ClassKit {

    private static Set<String> PATH_SET = new HashSet<String>();
    private static Set<Class> CLASS_SET = new HashSet<Class>();

    public static void loopClasses(ClassCatch catchImpl){
        for (Class clazz: getClasses()){
            catchImpl.doCatch(clazz);
        }
    }

    public static Set<String> getPaths(){
        if (PATH_SET.size() == 0){
            ClassKit.loopPath(new File(PathKit.getRootClassPath()));
        }
        return PATH_SET;
    }

    public static Set<Class> getClasses(){
        if (CLASS_SET.size() == 0){
            ClassKit.loopPath(new File(PathKit.getRootClassPath()));
        }
        return CLASS_SET;
    }



    private static void loopPath(File file){
        if (file.isDirectory()){
            for (File f: file.listFiles()){
                loopPath(f);
            }
        }else {
            PATH_SET.add(file.getAbsolutePath());
            String className = file.getAbsolutePath().replaceAll(PathKit.getRootClassPath() + "/", "").replaceAll("/", ".").replaceAll(".class", "");
            Class clazz = null;
            try {
                clazz = Class.forName(className);
                CLASS_SET.add(clazz);
            } catch (ClassNotFoundException e) {
                Log.e(e);
            }
        }
    }
}
